<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of category_model
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Admin_location_model extends CI_Model {

    //put your code here


    function __construct() {
        parent::__construct();
        log_message('debug', 'Init Model Initialized');
    }

    public function country_list($num=NULL, $offset=NULL) {
        $this->db->select('*')->from('country');
        $this->db->where('status', '51');
        $this->db->order_by('country');
        $this->db->limit($num, $offset);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    function get_country_name($country=NULL) {
        if ($country) {
            $code = trim($country);
            $sql = "Select * from country where id='$code'";
            $query = $this->db->query($sql);
            $row = $query->row();
            if ($query->num_rows == 1)
                return $row->country;
        }
    }

    function get_state_name($state=NULL) {
        if ($state) {
            $code = trim($state);
            $sql = "Select * from state where id='$code'";
            $query = $this->db->query($sql);
            $row = $query->row();
            if ($query->num_rows == 1)
                return $row->statename;
        }
    }

    function get_city_name($city=NULL) {
        if ($city) {
            $code = trim($city);
            $sql = "Select * from city where id='$code'";
            $query = $this->db->query($sql);
            $row = $query->row();
            if ($query->num_rows == 1)
                return $row->city;
        }
    }

    public function country_update($id=NULL, $detail=NULL) {
        $data = array('country' => $detail);
        $this->db->where('id', $id);
        $this->db->update('country', $data);
    }

    public function country_delete($id=NULL) {
        $this->db->where('id', $id);
        $this->db->delete('country');
        $this->db->where('countryid', $id);
        $this->db->delete('state');
        $this->db->where('countryid', $id);
        $this->db->delete('city');
        /* $this->db->where('countryid', $id);
          $this->db->delete('locality'); */
    }

    public function country_insert($detail=NULL) {
        $data = array('country' => $detail);
        $this->db->insert('country', $data);
    }

    public function state_list($num=NULL, $offset=NULL, $id=NULL) {

        $this->db->select('*')->from('state');
        $this->db->where('countryid', $id);
        $this->db->order_by('statename');
        $this->db->limit($num, $offset);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    public function state_update($id=NULL, $detail=NULL) {
        $data = array('statename' => $detail);
        $this->db->where('id', $id);
        $this->db->update('state', $data);
    }

    public function state_insert($detail=NULL, $id=NULL) {
        $data = array('statename' => $detail, 'countryid' => $id);
        $this->db->insert('state', $data);
    }

    public function state_delete($id=NULL) {
        $this->db->where('id', $id);
        $this->db->delete('state');
        $this->db->where('stateid', $id);
        $this->db->delete('city');
        /* $this->db->where('stateid', $id);
          $this->db->delete('locality'); */
    }

    public function city_list($state_id=NULL, $limit=NULL, $offset=NULL) {

        $this->db->select('*')->from('city');
        $this->db->where('stateid', $state_id);
        $this->db->order_by('city');
        $this->db->limit($limit, $offset);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    public function city_update($id=NULL, $detail=NULL, $top=NULL, $buttom=NULL) {
        $data = array('city' => $detail, 'topdisplay' => $top, 'bottomdisplay' => $buttom);
        $this->db->where('id', $id);
        $this->db->update('city', $data);
    }

    public function city_delete($id=NULL) {
        $this->db->where('id', $id);
        $this->db->delete('city');
        /* $this->db->where('cityid', $id);
          $this->db->delete('locality'); */
    }

    public function city_insert($detail=NULL, $id=NULL, $country_id=NULL) {
        $data = array('city' => $detail, 'stateid' => $id, 'countryid' => $country_id);
        $this->db->insert('city', $data);
    }

    public function locality_list($num=NULL, $offset=NULL, $id=NULL) {

        $this->db->select('*')->from('locality');
        $this->db->where('cityid', $id);
        $this->db->order_by('locality');
        $this->db->limit($num, $offset);
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

    public function locality_delete($id=NULL) {
        $this->db->where('id', $id);
        $this->db->delete('locality');
    }

    public function locality_update($id=NULL, $detail=NULL) {
        $data = array('locality' => $detail);
        $this->db->where('id', $id);
        $this->db->update('locality', $data);
    }

    public function locality_insert($detail=NULL, $cityid=NULL, $state_id=NULL, $country_id=NULL) {
        $data = array('locality' => $detail, 'cityid' => $cityid, 'stateid' => $state_id, 'countryid' => $country_id);
        $this->db->insert('locality', $data);
    }

    function state_count($cat_id=NULL) {
        if ($cat_id) {
            $code = trim($cat_id);
            $sql = "Select count(*)as total from state where countryid='$code'";
            $query = $this->db->query($sql);
            $row = $query->row();
            if ($row->total != 0)
                return $row->total;
            else
                return '0';
        }
    }

    function city_count($cat_id=NULL) {
        if ($cat_id) {
            $code = trim($cat_id);
            $sql = "Select count(*)as total from city where stateid='$code'";
            $query = $this->db->query($sql);
            $row = $query->row();
            if ($row->total != 0)
                return $row->total;
            else
                return '0';
        }
    }

    function locality_count($cat_id=NULL) {
        if ($cat_id) {
            $code = trim($cat_id);
            $sql = "Select count(*)as total from locality where cityid='$code'";
            $query = $this->db->query($sql);
            $row = $query->row();
            if ($row->total != 0)
                return $row->total;
            else
                return '0';
        }
    }

}

